package com.hspedu.tankgame03;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/2/12
 * @time 18:57
 */
@SuppressWarnings("all")
public class MyPanel extends JPanel implements KeyListener ,Runnable {
    private Hero hero;
    /**
     * 后面有多线程
     * 使用Vector比ArrayList更合适
     */
    private java.util.List<EnemyTank> enemyTanks = new Vector<>();
    private int enemyTanksSize = 3;

    public MyPanel() {
        if (hero == null)
            this.hero = new Hero(400, 500);

        for (int i = 0; i < enemyTanksSize; i++) {
            EnemyTank enemyTank = new EnemyTank(100 * (i + 1), 100);
            enemyTank.setDirection(2);
            enemyTanks.add(enemyTank);
        }

        this.hero.setSpeed(5);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);

        g.fillRect(0, 0, 1000, 750);

        drawTank(hero.getX(), hero.getY(), g, hero.getDirection(), hero.getType());

        if (hero.bullet != null && hero.bullet.isAlive == true){
            g.draw3DRect(hero.bullet.x,hero.bullet.y,1,1,false);
        }

        for (int i = 0; i < enemyTanksSize; i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirection(), enemyTank.getType());
        }
    }


    /**
     * @param x         坦克的左上角x坐标
     * @param y         坦克的左上角y坐标
     * @param g         画笔
     * @param direction 坦克方向（上下左右）
     * @param type      坦克类型
     */
    public void drawTank(int x, int y, Graphics g, int direction, int type) {
        switch (type) {
            case 0:
                //敌方的坦克
                g.setColor(Color.cyan);
                break;
            case 1:
                //自己的坦克
                g.setColor(Color.yellow);
                break;
        }

        /**
         * 根据坦克方向绘制不同的性状
         * 0    上
         * 1    右
         * 2    下
         * 3    左
         *
         * 向右转之后，坦克最左上角的点
         * 与坦克向上时的最左上角的点是一个点
         *
         * 向下相对于向上，只用换下炮筒
         *
         * 左相对于右也是换个炮筒
         */
        switch (direction) {
            //向上
            case 0:
                //左轮
                g.fill3DRect(x, y, 10, 60, false);
                //右轮
                g.fill3DRect(x + 30, y, 10, 60, false);
                //中间
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                //圆
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y, x + 20, y + 30);
                break;
            case 1:
                //上轮
                g.fill3DRect(x, y, 60, 10, false);
                //下轮
                g.fill3DRect(x, y + 30, 60, 10, false);
                //中间盖子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                //圆
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x + 60, y + 20, x + 30, y + 20);
                break;
            case 2:
                //左轮
                g.fill3DRect(x, y, 10, 60, false);
                //右轮
                g.fill3DRect(x + 30, y, 10, 60, false);
                //中间
                g.fill3DRect(x + 10, y + 10, 20, 40, false);
                //圆
                g.fillOval(x + 10, y + 20, 20, 20);
                g.drawLine(x + 20, y + 60, x + 20, y + 30);
                break;
            case 3:
                //上轮
                g.fill3DRect(x, y, 60, 10, false);
                //下轮
                g.fill3DRect(x, y + 30, 60, 10, false);
                //中间盖子
                g.fill3DRect(x + 10, y + 10, 40, 20, false);
                //圆
                g.fillOval(x + 20, y + 10, 20, 20);
                g.drawLine(x, y + 20, x + 30, y + 20);
                break;
            default:
                System.out.println("暂时没有处理");
        }
    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    /**
     * 键盘被按下时调用此方法
     *
     * @param e
     */
    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            //改变坦克方向
            hero.setDirection(0);
            hero.moveUp();
        } else if (e.getKeyCode() == KeyEvent.VK_S) {
            hero.setDirection(2);
            hero.moveDown();
        } else if (e.getKeyCode() == KeyEvent.VK_A) {
            hero.setDirection(3);
            hero.moveLeft();
        } else if (e.getKeyCode() == KeyEvent.VK_D) {
            hero.setDirection(1);
            hero.moveRight();
        } else if (e.getKeyCode() == KeyEvent.VK_J) {
            hero.shootEnemyTank();
        }
        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.repaint();
        }
    }
}
